/*
 * @Author: wang cong wangc9832@163.com
 * @Date: 2023-05-15 15:20:18
 * @LastEditors: wang cong wangc9832@163.com
 * @LastEditTime: 2023-05-17 15:23:31
 * @FilePath: /ams-front/src/api/asset/amsDocumentManage.ts
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
import { BasicFetchResult } from '../model/baseModel';
import { defHttp } from '@/utils/http/axios';
import { downloadBlob } from '@/utils/file/download';
const api = '/amsDocument';
/**
 * 查询文档中心分页
 * @author yd
 * @date 2023-05-10 10:57:37
 */
export const page = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: api + '/page', params });
// 文档中心_根据资产id查询文档信息
export const getListByid = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({
    url: `${api}/assetDoc`,
    params,
  });

/**
 * 查询文档中心不分页
 * @author yd
 * @date 2023-05-10 10:57:37
 */
export const detail = (id: string) =>
  defHttp.get<BasicFetchResult<any>>({ url: `${api}/detail/${id}` });
/**
 * 添加文档中心
 * @author yd
 * @date 2023-05-10 10:57:37
 */
export const add = (params?: any) =>
  defHttp.post({ url: api + '/add', params });
/**
 * 修改文档中心
 * @author yd
 * @date 2023-05-10 10:57:37
 */
export const edit = (params?: any) =>
  defHttp.post({ url: api + '/edit', params });

/**
 * 删除文档中心
 * @author yd
 * @date 2023-05-10 10:57:37
 */
export const del = (ids: string[]) =>
  defHttp.post({ url: api + '/delete', params: ids });
/**
 * 导出文档中心
 * @author yd
 * @date 2023-05-10 10:57:37
 */
export const exp = (params?: any) => {
  return new Promise<void>((resolve, reject) => {
    defHttp
      .get(
        {
          responseType: 'blob',
          timeout: 60 * 1000 * 5,
          url: api + '/export',
          params,
        },
        { isReturnNativeResponse: true }
      )
      .then((res) => {
        downloadBlob(res);
        resolve();
      })
      .catch(() => {
        reject();
      });
  });
};
// 批量导出
export const downloadById = (params?: any) => {
  return new Promise<void>((resolve, reject) => {
    defHttp
      .post(
        {
          responseType: 'blob',
          timeout: 60 * 1000 * 5,
          url: api + '/download',
          params,
        },
        { isReturnNativeResponse: true }
      )
      .then((res) => {
        const filename = res.headers['content-disposition'];
        const blob = new Blob([res.data]);
        const downloadElement = document.createElement('a');
        const href = window.URL.createObjectURL(blob);
        downloadElement.href = href;
        downloadElement.download = decodeURIComponent(
          filename.split('fileName=')[1].replace(new RegExp('"', 'g'), '')
        );
        document.body.appendChild(downloadElement);
        downloadElement.click();
        document.body.removeChild(downloadElement);
        window.URL.revokeObjectURL(href);

        resolve();
      })
      .catch(() => {
        reject();
      });
  });
};
